/**
 * 
 */
package au.edu.swin.videoshop.ejb.bean;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import au.edu.swin.videoshop.bean.Customer;
import au.edu.swin.videoshop.persistence.CustomerEntity;
import au.edu.swin.videoshop.service.CustomerService;


/**
 * @author julie
 *
 */
@Stateless
public class CustomerServiceBean implements CustomerService{

	@PersistenceContext
	private EntityManager em;
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#addCustomer(au.edu.swin.videoshop.bean.Customer)
	 */
	public void addCustomer(Customer customer) {
		TransferService service = new TransferService();
		CustomerEntity customerEntity = service.toCustomerEntity(customer);
		em.persist(customerEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#updateCustomer(au.edu.swin.videoshop.bean.Customer)
	 */
	public void updateCustomer(Customer customer) {
		TransferService service = new TransferService();
		CustomerEntity customerEntity = service.toCustomerEntity(customer);
		em.merge(customerEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#markDeleteCustomer(long)
	 */
	public void markDeleteCustomer(long id) {
		CustomerEntity customerEntity = em.find(CustomerEntity.class, id);
		if (customerEntity != null) {
			customerEntity.setDeleted(true);
			em.merge(customerEntity);
		}
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#authenticate(java.lang.String, java.lang.String)
	 */
	public Customer authenticate(String userName, String password) {
		Query query = em.createNamedQuery("authenticateCustomer");
		query.setParameter("userName", userName);
		query.setParameter("password", password);
		
		try {
			CustomerEntity customerEntity = (CustomerEntity)query.getSingleResult();
					
			if (customerEntity != null) {
				TransferService service = new TransferService();
				return service.toCustomerTransferObject(customerEntity);
			}
		}
		catch (NoResultException ex) {
			
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#changePassword(java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean changePassword(String userName, String currentPassword, String password) {
		Query query = em.createNamedQuery("changeCustomerPassword");
		query.setParameter("userName", userName);
		query.setParameter("currentPassword", currentPassword);
		query.setParameter("password", password);
		
		return query.executeUpdate() == 1;
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#findCustomerByCustomerId(long)
	 */
	public Customer findCustomerByCustomerId(long id) {
		CustomerEntity customerEntity = em.find(CustomerEntity.class, id);
		if (customerEntity != null) {
			TransferService service = new TransferService();
			return service.toCustomerTransferObject(customerEntity);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.CustomerService#findAllCustomers()
	 */
	public List<Customer> findAllCustomers() {
		Query query = em.createNamedQuery("findAllCustomers");
		List list = query.getResultList();
		TransferService service = new TransferService();		
		return service.toCustomerTransferObjectList(list);
	}
}
